package com.sxbbc.plat.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sxbbc.common.core.entity.jz.AgAgent;
import com.sxbbc.common.core.enums.CustomerLevelEnums;
import com.sxbbc.common.core.enums.OrderStatusEnums;
import com.sxbbc.common.core.mapper.jz.*;
import com.sxbbc.common.core.service.IBAccountService;
import com.sxbbc.common.core.service.ICAccountService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sxbbc.common.basics.enums.LoginChannelEnums;
import com.sxbbc.common.basics.enums.ServiceEnums;
import com.sxbbc.common.basics.utils.date.DateUtil;
import com.sxbbc.common.basics.utils.statics.StaticUtils;
import com.sxbbc.common.core.dto.jz.QueryOrderMoneyVo;
import com.sxbbc.common.core.entity.jz.CCustomer;
import com.sxbbc.common.core.entity.jz.CPayLog;
import com.sxbbc.common.core.entity.jz.OrderMain;
import com.sxbbc.common.core.service.ICCustomerService;
import com.sxbbc.common.core.utils.BasePage;

/**
 * 平台统计图表 业务层
 * 
 * @author xiaoky
 * @date 2019-05-13
 */
@Service
public class StatisService {
	@Autowired
	private OrderMainMapper orderMainMapper;
	@Autowired
	private OrderRefundLogMapper orderRefundLogMapper;
	@Autowired
	private ICCustomerService customerService;
	@Autowired
	private CCustomerMapper customerMapper;
	@Autowired
	private CPayLogMapper cPayLogMapper;
	@Autowired
	private SysCashDetailsMapper sysCashDetailsMapper;
	@Autowired
	private SysDealDataMapper sysDealDataMapper;
	@Autowired
	private BBusinessMapper businessMapper;
	@Autowired
	private AgAgentMapper agAgentMapper;
	@Autowired
	private ICAccountService icAccountService;
	@Autowired
	private IBAccountService ibAccountService;

	/**
	 * 根据类型查询对应时间数据
	 * 
	 * @param type 类型——day,work,month
	 * @return RestResponse
	 */
	public Map<String, Object> queryRefundMoneyStatis(String type) {
		JSONArray dateArray = new JSONArray();
		// 订单销售额
		JSONArray orderMoneyArray = new JSONArray();
		// 退款金额
		JSONArray refundMoneyArray = new JSONArray();
		try {
			// 当前时间
			Date date = new Date();
			List<String> dateList = getDateListByType(type, date);
			QueryOrderMoneyVo vo = new QueryOrderMoneyVo();
			vo.setType(type);
			vo.setDate(date);
			// 循环时间集
			for (int i = 0; i< dateList.size(); i++) {
				String data = dateList.get(i);
				vo.setTime(data);
				BigDecimal orderMoney = orderMainMapper.queryOrderMoneyByTypeAndTime(vo);
				BigDecimal refundMoney = orderRefundLogMapper.queryRefundMoneyByTypeAndTime(data, type, date);
				refundMoneyArray.add(refundMoney);
				orderMoneyArray.add(orderMoney);
				if (StaticUtils.WEEK_DATA.equals(type)) {// 周
					dateArray.add("第" + (i+1) + "周");
				} else if (StaticUtils.MONTH_DATA.equals(type)) {// 月
					dateArray.add(data.substring(0, data.length()-3));
				}else {
					dateArray.add(data);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		JSONObject orderObj = new JSONObject();
		orderObj.put("name", "销售额");
		orderObj.put("data", orderMoneyArray);
		JSONObject refundObj = new JSONObject();
		refundObj.put("name", "退款额");
		refundObj.put("data", refundMoneyArray);

		JSONArray array = new JSONArray();
		array.add(orderObj);
		array.add(refundObj);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("date", dateArray);
		map.put("series", array);
		return map;
	}

	/**
	 * 获取月份日期数据
	 * @param time     时间
	 * @return 月份日期数据
	 */
	public List<String> getDayDate(Date time) {
		DateUtil dateutil = DateUtil.getInstance();
		List<String> dateList = new ArrayList<>();
		int maxDate = dateutil.getMouthnum(time);
		String year = dateutil.toDateString(time, "yyyy");
		String month = dateutil.toDateString(time, "MM");
		String now = year + "-" + month;
		for (int i = 1; i <= maxDate; i++) {
			String day = i < 9 ? ("0" + i) : i + "";
			String nowtime = now + "-" + day;
			dateList.add(nowtime);
		}
		return dateList;
	}

	/**
	 * 获取月份周数据
	 * @param time 时间
	 * @return 月份周数据
	 */
	public List<String> getWeekDate(Date time) {
		DateUtil dateutil = DateUtil.getInstance();
		List<String> weekList = new ArrayList<>(); 
		// 当前是第几周
		int locaknum = dateutil.getLocalweek(time);
		// 本月有多少周
		int num = dateutil.getLocalWeeknum(time);
		for (int i = 1; i <= num; i++) {
			if (i <= locaknum) {
				int a = locaknum - i;
				weekList.add(a+"");
			} else {
				int a = i - locaknum;
				weekList.add(-a+"");
			}
		}
		return weekList;
	}

	/**
	 * 获取年份月份数据
	 * @param time  时间
	 * @return 月份数据
	 */
	public List<String> getMonthDate (Date time) {
		DateUtil dateutil = DateUtil.getInstance();
		List<String> dateList = new ArrayList<>();
		int maxDate = 12;
		String year = dateutil.toDateString(time, "yyyy");
		for (int i = 1; i <= maxDate; i++) {
			String month = i < 9 ? ("0" + i) : i + "";
			String yearMonth = year + "-" + month + "-01";
			dateList.add(yearMonth);
		}
		return dateList;
	}
	
	/**
	 * 通过类型获得不同的时间集
	 * @param type day, week, month
	 * @param time 时间
	 * @return List 时间集
	 */
	public List<String> getDateListByType (String type, Date time) {
		List<String> dateList = new ArrayList<>();
		if (StaticUtils.DAY_DATA.equals(type)) {// 日期数据
			dateList = getDayDate(time);
		} else if (StaticUtils.WEEK_DATA.equals(type)) {// 周数据
			dateList = getWeekDate(time);
		} else if (StaticUtils.MONTH_DATA.equals(type)) {// 月数据
			dateList = getMonthDate(time);
		}
		return dateList;
	}
	
	/**
	 * 获取用户饼状图数据
	 * @return
	 * @author xiaoky
	 */
	public List<Map<String, Object>> getCustomerFormPieData () {
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		for (LoginChannelEnums channel:LoginChannelEnums.queryValidList()) {
			Map<String, Object> map = new HashMap<String, Object>();
			QueryWrapper<CCustomer> queryWrapper = new QueryWrapper<CCustomer>();
			queryWrapper.eq("register_channel", channel.getChannel());
			int count = customerService.count(queryWrapper);
			map.put("value", count);
			map.put("name", channel.getName());
			list.add(map);
		}
		return list;
	}

	/**
	 * 获取用户折线图信息
	 * @param type 1表示月  2表示
	 * @return
	 * @author xiaoky
	 */
	public Map<String, Object> queryCustomerStatisDiffline(String type) {
		JSONArray dateArray = new JSONArray();
		// app
		JSONArray appCountArray = new JSONArray();
		// Wechat
		JSONArray wxCountArray = new JSONArray();
		// PC
		JSONArray pcCountArray = new JSONArray();
		// program
		JSONArray programCountArray = new JSONArray();

		try {
			// 当前时间
			Date time = new Date();
			List<String> dateList = getDateListByType(type, time);
			// 循环时间集
			for (int i = 0; i< dateList.size(); i++) {
				String data = dateList.get(i);
				int appCount = customerMapper.queryCustomerNumByChannelAndTime(data,type, LoginChannelEnums.APP.getChannel());
				int wxCount = customerMapper.queryCustomerNumByChannelAndTime(data,type, LoginChannelEnums.WX.getChannel());
				int pcCount = customerMapper.queryCustomerNumByChannelAndTime(data,type, LoginChannelEnums.PC.getChannel());
				int programChannel = customerMapper.queryCustomerNumByChannelAndTime(data,type, LoginChannelEnums.PROGRAM.getChannel());
				if(StaticUtils.DAY_DATA.equals(type)){
					dateArray.add(data);
				}else if(StaticUtils.MONTH_DATA.equals(type)){
					dateArray.add(data.substring(0, data.length()-3));
				}
				appCountArray.add(appCount);
				wxCountArray.add(wxCount);
				pcCountArray.add(pcCount);
				programCountArray.add(programChannel);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		JSONObject appObj = new JSONObject();
		appObj.put("name", LoginChannelEnums.APP.getName());
		appObj.put("data", appCountArray);
		JSONObject wxObj = new JSONObject();
		wxObj.put("name", LoginChannelEnums.WX.getName());
		wxObj.put("data", wxCountArray);
		JSONObject pcObj = new JSONObject();
		pcObj.put("name", LoginChannelEnums.PC.getName());
		pcObj.put("data", pcCountArray);

		JSONObject programObj = new JSONObject();
		programObj.put("name", LoginChannelEnums.PROGRAM.getName());
		programObj.put("data", programCountArray);
		
		JSONArray array = new JSONArray();
		array.add(appObj);
		array.add(wxObj);
		array.add(pcObj);
		array.add(programObj);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("date", dateArray);
		map.put("series", array);
		return map;
	}


	/**
	 * 查询平台欢迎页数据
	 * @return
	 */
	public Map<String, Object> queryWelcomeData() {
		Map<String, Object> map = new HashMap<>();

		//充值金额统计
		CPayLog payLog = new CPayLog();
		Map<String, Object> queryMap = new HashMap<>();
		payLog.setServiceType(ServiceEnums.RECHARGE_MONEY.getId());
		queryMap.put("payLog",payLog);
		this.queryRechargeData(queryMap,map);

		//提现金额统计
		this.queryCashData(queryMap,map);

		// 贡献值统计
		this.queryDonateData(queryMap,map);

		// 共享值统计
		this.queryShareData(queryMap,map);

		// 订单数量统计
		QueryOrderMoneyVo vo = new QueryOrderMoneyVo();
		this.queryOrderNumData(map,vo);

		// 完成订单金额统计
		this.queryFinishOrderMoneyData(map,vo);

		// 退款订单数量统计
		this.queryOrderRefundNumData(map,vo);

		// 退款订单金额统计
		this.queryOrderRefundMoneyData(map,vo);

		// 会员数量统计
		this.queryCustomerNumData(queryMap,map);

		// 商家数量统计
		this.queryBussinessNumData(queryMap,map);

		//其他统计数量
		this.queryOtherDiffNumData(map);
		return map;
	}

	/**
	 * 其他统计数量
	 */
	private void queryOtherDiffNumData(Map<String, Object> map) {
		//创业会员总数
		QueryWrapper<CCustomer> wrapper = new QueryWrapper<>();
		wrapper.eq("levelid", CustomerLevelEnums.SMALL.getId());
		Integer bussinessNum = customerMapper.selectCount(wrapper);
		map.put("bussinessNum",bussinessNum);

		//普通会员总数
		wrapper = new QueryWrapper<>();
		wrapper.eq("levelid", CustomerLevelEnums.NORMAL.getId());
		Integer normalNum = customerMapper.selectCount(wrapper);
		map.put("normalNum",normalNum);

		//总代理数
		QueryWrapper<AgAgent> agentQueryWrapper = new QueryWrapper<>();
		agentQueryWrapper.eq("status",StaticUtils.STATUS_SUCCESS);
		Integer agentNum = agAgentMapper.selectCount(agentQueryWrapper);
		map.put("agentNum",agentNum);

		//余额总额
		BigDecimal cSumAccount = icAccountService.sumAccount();
		map.put("cSumAccount",cSumAccount);

		//交易账户总额
		BigDecimal bSumAccount = ibAccountService.symAccount();
		map.put("bSumAccount",bSumAccount);

	}

	/**
	 * 商家数量统计
	 * @param queryMap
	 * @param map
	 */
	private void queryBussinessNumData(Map<String, Object> queryMap, Map<String, Object> map) {
		queryMap = new HashMap<>();
		//商家数量
		int totalBussinessNum = businessMapper.sumBussinessNum(queryMap);
		map.put("totalBussinessNum", totalBussinessNum);

		// 本月商家数量
		queryMap.put("dateType", StaticUtils.MONTH_DATA);
		int bussinessNumByMonth = businessMapper.sumBussinessNum(queryMap);
		map.put("bussinessNumByMonth", bussinessNumByMonth);

		// 当天商家数量
		queryMap.put("dateType", StaticUtils.DAY_DATA);
		int bussinessNumByDay = businessMapper.sumBussinessNum(queryMap);
		map.put("bussinessNumByDay", bussinessNumByDay);
	}

	/**
	 * 会员数量统计
	 * @param queryMap
	 * @param map
	 */
	private void queryCustomerNumData(Map<String, Object> queryMap, Map<String, Object> map) {
		queryMap = new HashMap<>();
		//会员总数
		int totalCustomerNum = customerMapper.sumCustomerNum(queryMap);
		map.put("totalCustomerNum", totalCustomerNum);

		// 本月会员数量
		queryMap.put("dateType", StaticUtils.MONTH_DATA);
		int customerNumByMonth = customerMapper.sumCustomerNum(queryMap);
		map.put("customerNumByMonth", customerNumByMonth);

		// 当天会员数量
		queryMap.put("dateType", StaticUtils.DAY_DATA);
		int customerNumByDay = customerMapper.sumCustomerNum(queryMap);
		map.put("customerNumByDay", customerNumByDay);
	}

	/**
	 * 退款订单金额统计
	 * @param map
	 * @param vo
	 */
	private void queryOrderRefundMoneyData(Map<String, Object> map, QueryOrderMoneyVo vo) {
		vo = new QueryOrderMoneyVo();
		vo.setOrderStatus(OrderStatusEnums.REFUND.getId());

		//退款订单金额总计
		BigDecimal totalOrderRefundMoney = orderMainMapper.queryOrderMoneyByTypeAndTime(vo);
		map.put("totalOrderRefundMoney", totalOrderRefundMoney);

		//当月退款订单金额
		vo.setType(StaticUtils.MONTH_DATA);
		vo.setDate(new Date());
		vo.setTime(DateUtil.getInstance().toDateString(new Date()));
		BigDecimal orderRefundMoneyByMonth = orderMainMapper.queryOrderMoneyByTypeAndTime(vo);
		map.put("orderRefundMoneyByMonth", orderRefundMoneyByMonth);

		//当日退款订单金额
		vo.setType(StaticUtils.DAY_DATA);
		vo.setTime(DateUtil.getInstance().toDateString(new Date()));
		BigDecimal orderRefundMoneyByDay = orderMainMapper.queryOrderMoneyByTypeAndTime(vo);
		map.put("orderRefundMoneyByDay", orderRefundMoneyByDay);
	}

	/**
	 * 退款订单数量统计
	 * @param map
	 * @param vo
	 */
	private void queryOrderRefundNumData(Map<String, Object> map, QueryOrderMoneyVo vo) {
		vo = new QueryOrderMoneyVo();
		vo.setOrderStatus(OrderStatusEnums.REFUND.getId());

		//退款订单数量总计
		int totalOrderReFundNum = orderMainMapper.countOrderNumByTypeAndTime(vo);
		map.put("totalOrderReFundNum", totalOrderReFundNum);

		//当月退款订单数量
		vo.setType(StaticUtils.MONTH_DATA);
		vo.setDate(new Date());
		vo.setTime(DateUtil.getInstance().toDateString(new Date()));
		int orderReFundNumByMonth = orderMainMapper.countOrderNumByTypeAndTime(vo);
		map.put("orderReFundNumByMonth", orderReFundNumByMonth);

		//当日退款订单数量
		vo.setType(StaticUtils.DAY_DATA);
		vo.setTime(DateUtil.getInstance().toDateString(new Date()));
		int orderReFundNumByDay = orderMainMapper.countOrderNumByTypeAndTime(vo);
		map.put("orderReFundNumByDay", orderReFundNumByDay);
	}

	/**
	 * 完成订单金额统计
	 * @param map
	 * @param vo
	 */
	private void queryFinishOrderMoneyData(Map<String, Object> map, QueryOrderMoneyVo vo) {
		vo = new QueryOrderMoneyVo();
		vo.setOrderStatus(OrderStatusEnums.OVER.getId());
		//完成订单金额总计
		BigDecimal totalFinishOrderMoney = orderMainMapper.queryOrderMoneyByTypeAndTime(vo);
		map.put("totalFinishOrderMoney", totalFinishOrderMoney);

		//当月完成订单金额
		vo.setType(StaticUtils.MONTH_DATA);
		BigDecimal finishOrderMoneyByMonth = orderMainMapper.queryOrderMoneyByTypeAndTime(vo);
		map.put("finishOrderMoneyByMonth", finishOrderMoneyByMonth);

		//当日完成订单金额
		vo.setType(StaticUtils.DAY_DATA);
		BigDecimal finishOrderMoneyByDay = orderMainMapper.queryOrderMoneyByTypeAndTime(vo);
		map.put("finishOrderMoneyByDay", finishOrderMoneyByDay);
	}

	/**
	 * 订单数量统计
	 * @param map
	 * @param vo
	 */
	private void queryOrderNumData(Map<String, Object> map, QueryOrderMoneyVo vo) {
		//订单数量总计
		int totalOrderNum = orderMainMapper.countOrderNumByTypeAndTime(vo);
		map.put("totalOrderNum", totalOrderNum);

		//当月订单数量
		vo.setType(StaticUtils.MONTH_DATA);
		vo.setDate(new Date());
		vo.setTime(DateUtil.getInstance().toDateString(new Date()));
		int orderNumByMonth = orderMainMapper.countOrderNumByTypeAndTime(vo);
		map.put("orderNumByMonth", orderNumByMonth);

		//当日订单数量
		vo.setType(StaticUtils.DAY_DATA);
		vo.setTime(DateUtil.getInstance().toDateString(new Date()));
		int orderNumByDay = orderMainMapper.countOrderNumByTypeAndTime(vo);
		map.put("orderNumByDay", orderNumByDay);
	}

	/**
	 * 共享值统计
	 * @param queryMap
	 * @param map
	 */
	private void queryShareData(Map<String, Object> queryMap, Map<String, Object> map) {
		queryMap = new HashMap<>();
		queryMap.put("type", "send_total_share");
		//共享值总数
		BigDecimal totalShareNum = sysDealDataMapper.sumDataNum(queryMap);
		map.put("totalShareNum", totalShareNum);

		//当月共享值
		queryMap.put("dateType", StaticUtils.MONTH_DATA);
		queryMap.put("date", new Date());
		BigDecimal shareNumByMonth = sysDealDataMapper.sumDataNum(queryMap);
		map.put("shareNumByMonth", shareNumByMonth);

		//当天共享值
		queryMap.put("dateType", StaticUtils.DAY_DATA);
		queryMap.put("date", new Date());
		BigDecimal shareNumByDay = sysDealDataMapper.sumDataNum(queryMap);
		map.put("shareNumByDay", shareNumByDay);
	}

	/**
	 * 贡献值统计
	 * @param queryMap
	 * @param map
	 */
	private void queryDonateData(Map<String, Object> queryMap, Map<String, Object> map) {
		queryMap = new HashMap<>();
		queryMap.put("type", "send_total_donate");

		//贡献值总数
		BigDecimal totalDonateNum = sysDealDataMapper.sumDataNum(queryMap);
		map.put("totalDonateNum", totalDonateNum);

		//当月贡献值
		queryMap.put("dateType", StaticUtils.MONTH_DATA);
		queryMap.put("date", new Date());
		BigDecimal donateNumByMonth = sysDealDataMapper.sumDataNum(queryMap);
		map.put("donateNumByMonth", donateNumByMonth);

		//当天贡献值
		queryMap.put("dateType", StaticUtils.DAY_DATA);
		queryMap.put("date", new Date());
		BigDecimal donateNumByDay = sysDealDataMapper.sumDataNum(queryMap);
		map.put("donateNumByDay", donateNumByDay);
	}

	/**
	 * 提现金额统计
	 * @param queryMap
	 * @param map
	 */
	private void queryCashData(Map<String, Object> queryMap, Map<String, Object> map) {
		queryMap = new HashMap<>();
		// 提现金额总额
		BigDecimal totalCashMoney = sysCashDetailsMapper.sumRealMoney(queryMap);
		map.put("totalCashMoney", totalCashMoney);

		// 本月实际提现金额
		queryMap.put("dateType", StaticUtils.MONTH_DATA);
		BigDecimal cashMoneyByMonth = sysCashDetailsMapper.sumRealMoney(queryMap);
		map.put("cashMoneyByMonth", cashMoneyByMonth);

		// 当日实际提现金额
		queryMap.put("dateType", StaticUtils.DAY_DATA);
		BigDecimal cashMoneyByDay = sysCashDetailsMapper.sumRealMoney(queryMap);
		map.put("cashMoneyByDay", cashMoneyByDay);

	}

	/**
	 * 充值金额统计
	 * @param queryMap
	 */
	private void queryRechargeData(Map<String, Object> queryMap,Map<String, Object> map) {
		//总计充值金额
		BigDecimal totalRechargeMoney = cPayLogMapper.sumPayMoney(queryMap);
		map.put("totalRechargeMoney", totalRechargeMoney);

		// 本月充值金额
		queryMap.put("dateType", StaticUtils.MONTH_DATA);
		BigDecimal rechargeMoneyByMonth = cPayLogMapper.sumPayMoney(queryMap);
		map.put("rechargeMoneyByMonth", rechargeMoneyByMonth);

		// 当天充值金额
		queryMap.put("dateType", StaticUtils.DAY_DATA);
		BigDecimal rechargeMoneyByDay = cPayLogMapper.sumPayMoney(queryMap);
		map.put("rechargeMoneyByDay", rechargeMoneyByDay);
	}

	/**
	 * 查询贡献值和共享值统计
	 * @return 贡献值和共享值数据
	 * @author xiaoky
	 */
	public Map<String, Object> queryAccountStatis() {
		JSONArray dateArray = new JSONArray();
		// 当日总贡献值
		JSONArray donateArray = new JSONArray();
		// 发放共享值
		JSONArray shareArray = new JSONArray();
		try {
			String type = StaticUtils.DAY_DATA;
			// 获取当前时间
			Date date = DateUtil.getInstance().currentDate();
			List<String> dateList = getDateListByType(type, date);
			Map<String, Object> queryMap = new HashMap<String, Object>();
			queryMap.put("dateType", type);
			// 循环时间集
			for (int i = 0; i< dateList.size(); i++) {
				String data = dateList.get(i);
				queryMap.put("date", data);
				queryMap.put("type", "send_total_donate");
				BigDecimal donateNum = sysDealDataMapper.sumDataNum(queryMap);
				queryMap.put("type", "send_total_share");
				BigDecimal shareNum = sysDealDataMapper.sumDataNum(queryMap);
				donateArray.add(donateNum);
				shareArray.add(shareNum);
				dateArray.add(data);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		JSONObject donateObj = new JSONObject();
		donateObj.put("name", "总贡献值");
		donateObj.put("data", donateArray);
		JSONObject shareObj = new JSONObject();
		shareObj.put("name", "发放共享值");
		shareObj.put("data", shareArray);

		JSONArray array = new JSONArray();
		array.add(donateObj);
		array.add(shareObj);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("date", dateArray);
		map.put("series", array);
		return map;
	}
	
	
	/**
	 * 查询订单、退款数量
	 * @param type 类型
	 * @return
	 * @author xiaoky
	 */
	public Map<String, Object> queryRefundNumStatis(String type) {
		JSONArray dateArray = new JSONArray();
		// 订单销售额
		JSONArray orderNumArray = new JSONArray();
		// 退款金额
		JSONArray refundNumArray = new JSONArray();
		try {
			// 当前时间
			Date date = new Date();
			List<String> dateList = getDateListByType(type, date);
			QueryOrderMoneyVo vo = new QueryOrderMoneyVo();
			vo.setType(type);
			vo.setDate(date);
			// 循环时间集
			for (int i = 0; i< dateList.size(); i++) {
				String data = dateList.get(i);
				vo.setTime(data);
				int orderNum = orderMainMapper.queryOrderNumByTypeAndTime(vo);
				int refundNum = orderRefundLogMapper.queryRefundNumByTypeAndTime(data, type, date);
				refundNumArray.add(refundNum);
				orderNumArray.add(orderNum);
				if (StaticUtils.WEEK_DATA.equals(type)) {// 周
					dateArray.add("第" + (i+1) + "周");
				} else if (StaticUtils.MONTH_DATA.equals(type)) {// 月
					dateArray.add(data.substring(0, data.length()-3));
				}else {
					dateArray.add(data);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		JSONObject orderObj = new JSONObject();
		orderObj.put("name", "销售数");
		orderObj.put("data", orderNumArray);
		JSONObject refundObj = new JSONObject();
		refundObj.put("name", "退款数");
		refundObj.put("data", refundNumArray);

		JSONArray array = new JSONArray();
		array.add(orderObj);
		array.add(refundObj);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("date", dateArray);
		map.put("series", array);
		return map;
	}

	/**
	 * 查询本月产品销量统计数据(前十)
	 * @param date 时间
	 * @return
	 * @author xiaoky
	 */
	public List<Map<String, Object>> queryProSaleStatis(String date) {
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try {
			DateUtil dateUtil = DateUtil.getInstance();
			if (StringUtils.isBlank(date)) {
				// 取得当前时间
				date = dateUtil.nowDate(dateUtil.DEFAULT_DATE_PATTERN);
			}
			BasePage<OrderMain> page = new BasePage<OrderMain>();
			IPage<Map<String, Object>> pageInfo = orderMainMapper.queryMonthSalePro(page, date);
			list = pageInfo.getRecords();
		} catch (Exception e) {
			e.printStackTrace();
			return list;
		}
		return list;
	}



	/**
	 * 查询订单、退款数量
	 * @param type 类型
	 * @return
	 * @author xiaoky
	 */
	public Map<String, Object> queryOrderNumStatis(String type) {
		JSONArray dateArray = new JSONArray();
		// app
		JSONArray appCountArray = new JSONArray();
		// Wechat
		JSONArray wxCountArray = new JSONArray();
		// PC
		JSONArray pcCountArray = new JSONArray();
		// program
		JSONArray programCountArray = new JSONArray();

		try {
			// 当前时间
			Date time = new Date();
			List<String> dateList = getDateListByType(type, time);
			// 循环时间集
			for (int i = 0; i< dateList.size(); i++) {
				String data = dateList.get(i);
				QueryOrderMoneyVo vo = new QueryOrderMoneyVo();
				vo.setType(type);
				vo.setTime(data);
				vo.setFromChannel(LoginChannelEnums.APP.getChannel());
				int appCount = orderMainMapper.queryOrderNumByTypeAndTime(vo);
				vo.setFromChannel(LoginChannelEnums.WX.getChannel());
				int wxCount = orderMainMapper.queryOrderNumByTypeAndTime(vo);
				vo.setFromChannel(LoginChannelEnums.PC.getChannel());
				int pcCount = orderMainMapper.queryOrderNumByTypeAndTime(vo);
				vo.setFromChannel(LoginChannelEnums.PROGRAM.getChannel());
				int programChannel = orderMainMapper.queryOrderNumByTypeAndTime(vo);
				if(StaticUtils.DAY_DATA.equals(type)){
					dateArray.add(data);
				}else if(StaticUtils.MONTH_DATA.equals(type)){
					dateArray.add(data.substring(0, data.length()-3));
				}
				appCountArray.add(appCount);
				wxCountArray.add(wxCount);
				pcCountArray.add(pcCount);
				programCountArray.add(programChannel);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		JSONObject appObj = new JSONObject();
		appObj.put("name", LoginChannelEnums.APP.getName());
		appObj.put("data", appCountArray);
		JSONObject wxObj = new JSONObject();
		wxObj.put("name", LoginChannelEnums.WX.getName());
		wxObj.put("data", wxCountArray);
		JSONObject pcObj = new JSONObject();
		pcObj.put("name", LoginChannelEnums.PC.getName());
		pcObj.put("data", pcCountArray);
		JSONObject programObj = new JSONObject();
		programObj.put("name", LoginChannelEnums.PROGRAM.getName());
		programObj.put("data", programCountArray);

		JSONArray array = new JSONArray();
		array.add(appObj);
		array.add(wxObj);
		array.add(pcObj);
		array.add(programObj);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("date", dateArray);
		map.put("series", array);
		return map;
	}
}
